package com.example.medical_project.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.medical_project.commons.annotation.Log;
import com.example.medical_project.commons.enums.BusinessTypeEnum;
import com.example.medical_project.commons.vo.AjaxResult;
import com.example.medical_project.modules.system.domain.DictData;
import com.example.medical_project.modules.system.dto.DictDataDto;
import com.example.medical_project.modules.system.service.DictDataService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Array;
import java.util.Arrays;


@RestController
@RequestMapping("/system/dict/data")
public class DictController {


    @Autowired
    private DictDataService dictDataService;

    @GetMapping("/listForPage")
    public AjaxResult listForPage(DictDataDto dictDataDto){

        Page<DictData> page = dictDataService.listForPage(dictDataDto);

        return AjaxResult.success(page.getRecords(), page.getTotal());
    }


    /**
     * 根据dictId查询字典数据
     * @param dictId 字典id
     * @return
     */
    @GetMapping("/getOne/{dictId}")
    public AjaxResult getDictData(@PathVariable @Validated
                                  @NotNull(message = "字典数据ID不能为空") Long dictId){
        return AjaxResult.success(dictDataService.getById(dictId));
    }

    /**
     * 新增字典数据
     * @param params
     * @return
     */
    @PostMapping("/addDictData")
    public AjaxResult addDictData(@Validated DictDataDto params){

        dictDataService.addDictData(params);

        return AjaxResult.success();
    }

    /**
     * 修改字典数据
     * @param params
     * @return
     */
    @PutMapping("/updateDictData")
    @Log(title="修改字典数据",businessType = BusinessTypeEnum.UPDATE)
    public AjaxResult updateDictData(DictDataDto params){

        dictDataService.updateDictData(params);

        return AjaxResult.success();
    }


//    根据dictId删除字典数据

    @DeleteMapping("/deleteDictDataByIds/{dictIds}")
    @Log(title = "删除字典数据",businessType = BusinessTypeEnum.DELETE)
    public AjaxResult deleteDictByIds(@PathVariable Long[] dictIds){

        dictDataService.removeBatchByIds(Arrays.asList(dictIds));

        return AjaxResult.success();
    }

}
